<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>Login Page</title>
    <!-- 样 式 文 件 -->
    <link rel="stylesheet" th:href="@{component/pear/css/pear.css}"/>
    <link rel="stylesheet" th:href="@{admin/css/other/login.css}"/>

</head>
<!-- 代 码 结 构 -->
<body background="admin/images/background.svg" style="background-size: cover;">
<form action="javascript:void(0);" class="layui-form" lay-filter="example">
    <div class="layui-form-item">
        <img class="logo" th:src="@{admin/images/logo.png}"/>
        <div class="title">Pear Admin</div>
        <div class="desc">
            明 湖 区 最 具 影 响 力 的 设 计 规 范 之 一
        </div>
    </div>
    <div class="layui-form-item">
        <input class="layui-input" hover id="username" lay-verify="required" name="username" placeholder="请输入用户名"/>
    </div>
    <div class="layui-form-item">
        <input class="layui-input" hover id="password" lay-verify="required" name="password" placeholder="请输入密码"/>
    </div>
    <div class="layui-form-item">
        <input class="code layui-input layui-input-inline" hover id="code" lay-verify="required" name="code"
               placeholder="验证码 : "/>
        <img class="codeImage" onclick="this.src='/code/image?'+Math.random()" th:src="@{/code/image}"/>
    </div>
    <div class="layui-form-item">
        <input checked id="rememberMe" lay-skin="primary" name="rememberMe" title="记住密码" type="checkbox">
    </div>
    <div class="layui-form-item">
        <button class="pear-btn pear-btn-success login" id="login" lay-filter="login" lay-submit type="button">
            登 入
        </button>
    </div>
</form>
<!-- 资 源 引 入 -->
<script th:src="@{https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js}" type="text/javascript"></script>
<script th:src="@{component/layui/layui.js}"></script>
<script th:src="@{component/pear/pear.js}"></script>
<script>
    layui.use(['form', 'button', 'popup', 'http'], function () {
        var form = layui.form;
        var button = layui.button;
        var popup = layui.popup;
        var http = layui.http;

        // 登 录 提 交
        form.on('submit(login)', function (data) {
            var username = $('#username').val();
            var password = $('#password').val();
            var code = $('#code').val();
            var rememberMe = $('#rememberMe').prop('checked');
            /// 验证
            // var data1 = form.val("example");
            // /// 登录
            console.log(rememberMe)
            button.load({
                elem: '.login',
            })
            $.ajax({
                    url: '/doLogin',
                    type: 'post',
                    data: {
                        "username": username,
                        "password": password,
                        "code": code,
                        "remember-me": rememberMe
                    }, success: function (res, status) {
                        $('#login').attr("disabled", false)
                        $('#login').html('登入');
                        if (res.code === 401) {
                            // console.log(res)
                            // console.log(status)
                            layer.msg(res.message,
                                {icon: 2, time: 3000})
                        } else if (res.code === 200) {
                            // console.log(res)
                            $('#login').html('登入成功')
                            layer.msg(res.message + ',2秒后跳转...',
                                {icon: 1, time: 2000}, function () {
                                    location.href = res.data
                                })
                        }
                    }
                }
            )
            // /// 动画
            // button.load({
            //     elem: '.login',
            //     time: 1500,
            //
            //     done: function () {
            //         popup.success("登录成功", function () {
            //             console.log('登录成功')
            //             // location.href = "index.html"
            //         });
            //     }
            // })
            return false;
        });
    })
</script>
</body>
</html>
